package com.revenant.driver.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.revenant.driver.R;
import com.revenant.driver.adapter.Driver.DriverOrderItemAdapter;
import com.revenant.driver.adapter.OrderItemAdapter;
import com.revenant.driver.base.BaseFragment;
import com.revenant.driver.bean.BaseBean.MessageEvent;
import com.revenant.driver.bean.Driver.SearchEmptyBean;
import com.revenant.driver.bean.ShipperBean.OrderListBean;
import com.revenant.driver.ui.XieYiActivity;
import com.revenant.driver.ui.activity.DriverShangChuanBangDanActivity;
import com.revenant.driver.ui.activity.OrderCarDetailActivity;
import com.revenant.driver.utils.GsonUtil;
import com.revenant.driver.utils.SPUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;
import butterknife.Unbinder;

import static com.revenant.driver.bean.BaseBean.MyEventCode.Order_Type_SIGAL;
import static com.revenant.driver.utils.Constants.Driver_Cancel_Order;
import static com.revenant.driver.utils.Constants.Driver_Cancel_Order_New;
import static com.revenant.driver.utils.Constants.Driver_Car_Add;
import static com.revenant.driver.utils.Constants.Order_list;
import static com.revenant.driver.utils.Constants.popMessage;

/**
 * @ProjectName: Driver
 * @Package: com.revenant.driver.ui.fragment
 * @ClassName: DriverFragment
 * @Description: java类作用描述
 * @Author: Revenant
 * @CreateDate: 2020-02-07 14:49
 * @UpdateUser: 更新者
 * @UpdateDate: 2020-02-07 14:49
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class DriverOrderFragment extends BaseFragment {
    private DriverOrderItemAdapter mOrderItemAdapter;
    Unbinder unbinder;
    @BindView(R.id.order_recyclerview)
    RecyclerView orderRecyclerview;
    @BindView(R.id.smartrefresh)
    SmartRefreshLayout smartrefresh;
    private int pagernum = 1;

    public DriverOrderFragment() {
        // Required empty public constructor
    }

    public static DriverOrderFragment newInstance(String param1) {
        DriverOrderFragment fragment = new DriverOrderFragment();
        Bundle args = new Bundle();
        args.putString(param1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_driverorder;
    }

    @Override
    protected void initView() {
        mOrderItemAdapter = new DriverOrderItemAdapter();
        orderRecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        orderRecyclerview.setAdapter(mOrderItemAdapter);
        smartrefresh.autoRefresh();
    }

    @Override
    public void initData() {

        smartrefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pagernum++;
                initDataRecyclerview("1");
                smartrefresh.finishLoadMore(true);//加载完成
            }
        });
        //刷新
        smartrefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mOrderItemAdapter.setNewData(null);
                pagernum = 1;
                initDataRecyclerview("1");
                smartrefresh.finishRefresh(true);//刷新完成
            }
        });
        mOrderItemAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (mOrderItemAdapter.getData().get(position).getStatus()) {
                    case 0:
                        switch (view.getId()) {
                            case R.id.cancel:
                                cancelOrder(String.valueOf(mOrderItemAdapter.getData().get(position).getId()), position);
                                break;
                            case R.id.order_click:
                                Bundle bundle = new Bundle();
                                bundle.putInt("uploadtype", 0);
                                bundle.putInt("uploadid", mOrderItemAdapter.getData().get(position).getId());
                                bundle.putString("orderno", mOrderItemAdapter.getData().get(position).getOrderNo());
                                bundle.putString("orderloadcode", mOrderItemAdapter.getData().get(position).getLoadingCode());
                                bundle.putString("orderdescode", mOrderItemAdapter.getData().get(position).getUnloadCode());
                                bundle.putString("orderamount", String.valueOf(mOrderItemAdapter.getData().get(position).getAmount()));
                                Intent intent = new Intent();
                                intent.putExtras(bundle);
                                intent.setClass(getActivity(), DriverShangChuanBangDanActivity.class);
                                startActivity(intent);
                                break;
                        }
                        break;
                    case 1:

                        selectPopMessage(getActivity(),popMessage.get(0),position);
                        break;

                    case 3:
                        break;
                    case 4:
                        break;
                }
            }
        });
        mOrderItemAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putInt("goods_id", mOrderItemAdapter.getData().get(position).getId());
                Intent intent = new Intent();
                intent.putExtras(bundle);
                intent.setClass(getActivity(), OrderCarDetailActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initDataRecyclerview(String isBill) {
        OkGo.<String>get(Order_list)

                .params("driverId", SPUtils.getAccounId(getActivity()))
                .params("pageNum", pagernum)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        OrderListBean listBean = GsonUtil.parseJsonWithGson(response.body(), OrderListBean.class);
                        int pageNum = listBean.getData().getPageNum();
                        int pages = listBean.getData().getPages();
                        if (listBean.getCode().equals("0")) {

                                if (pageNum == 1) {
                                    mOrderItemAdapter.setNewData(listBean.getData().getResult());
                                } else if (pageNum <= pages) {
                                    mOrderItemAdapter.addData(listBean.getData().getResult());
                                } else {
                                    mOrderItemAdapter.loadMoreEnd();
                                }


//

                        } else {
                            mOrderItemAdapter.loadMoreFail();
                        }

                    }
                });
    }

    private void cancelOrder(String id, int pos) {
        OkGo.<String>post(Driver_Cancel_Order_New)

                .params("orderId", id)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        SearchEmptyBean g = GsonUtil.parseJsonWithGson(response.body(), SearchEmptyBean.class);
                        if (g.getCode().equals("0")) {
                            mOrderItemAdapter.remove(pos);
                        }


                    }
                });
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected void receiveEvent(MessageEvent event) {
        super.receiveEvent(event);
        switch (event.getCode()) {
            case Order_Type_SIGAL:
                smartrefresh.autoRefresh();
                break;
        }
    }

    private void selectPopMessage(Context context,String message,int position){
        AlertDialog alertDialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = getLayoutInflater().inflate(R.layout.popselectmessage, null);
        TextView text=view.findViewById(R.id.popselectmessage);
        text.setText(message);

        TextView cancel=view.findViewById(R.id.cancel);
        TextView ok=view.findViewById(R.id.confirm);

        builder.setView(view);

        alertDialog = builder.show();

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();

            }
        });

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                selectBangDan(position);
            }
        });
    }


    private void selectBangDan(int position){
        Bundle bundle = new Bundle();
        bundle.putInt("uploadtype", 1);
        bundle.putInt("uploadid", mOrderItemAdapter.getData().get(position).getId());
        bundle.putString("orderno", mOrderItemAdapter.getData().get(position).getOrderNo());
        bundle.putString("orderloadcode", mOrderItemAdapter.getData().get(position).getLoadingCode());
        bundle.putString("orderdescode", mOrderItemAdapter.getData().get(position).getUnloadCode());
        bundle.putString("orderamount", String.valueOf(mOrderItemAdapter.getData().get(position).getAmount()));
        Intent intent = new Intent();
        intent.putExtras(bundle);
        intent.setClass(getActivity(), DriverShangChuanBangDanActivity.class);
        startActivity(intent);
    }


}
